package com.smartlbs.baseframe.utils;

import com.alibaba.fastjson.JSON;
import com.smartlbs.entity.GameInfo;
import com.smartlbs.entity.OtherBet;
import com.smartlbs.entity.OtherBet.GE;
import com.smartlbs.entity.OtherBet.betPlayer;
import com.smartlbs.entity.OtherMatch.player;
import com.smartlbs.vo.ExceBetInfoVo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class ParseExcelUtil {
    /**
     * 大赛事 中文英文列表
     *
     * @return
     */

    public static Map<String, String> getGameNames() {
        Workbook wb = null;
        Sheet sheet = null;
        Row row = null;
        Map<String, String> map = new LinkedHashMap<String, String>();

        String cellData = null;
        String filePath = "/configs/excel/gameinfo.xlsx";
        wb = readExcel(filePath);
        if (wb != null) {
            // 用来存放表中数据
            // 获取第一个sheet
            sheet = wb.getSheetAt(0);
            // 获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            // 获取第一行
            row = sheet.getRow(0);
            // 获取最大列数
            int colnum = row.getPhysicalNumberOfCells();

            for (int i = 1; i < rownum; i++) {
                row = sheet.getRow(i);
                if (row != null) {
                    String key = (String) getCellFormatValue(row.getCell(3));
                    map.put(key.toLowerCase(), (String) getCellFormatValue(row.getCell(2)));

                } else {
                    break;
                }

            }
        }
        return map;
    }

    /**
     * 球员名中英文对应
     *
     * @return
     */
    public static Map<String, String> getPlayerNames() {
        Workbook wb = null;
        Sheet sheet = null;
        Map<String, String> map = new LinkedHashMap<String, String>();

        Row row = null;
        String cellData = null;
        String filePath = "/configs/excel/gameinfo.xlsx";
        wb = readExcel(filePath);
        if (wb != null) {

            // 获取第一个sheet
            sheet = wb.getSheetAt(1);
            // 获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            // 获取第一行
            row = sheet.getRow(0);
            // 获取最大列数
            int colnum = row.getPhysicalNumberOfCells();
            for (int i = 1; i < rownum; i++) {
                row = sheet.getRow(i);
                if (row != null) {
                    String key = (String) getCellFormatValue(row.getCell(3));
                    map.put(key.toLowerCase(), (String) getCellFormatValue(row.getCell(2)));

                } else {
                    break;
                }

            }
        }
        return map;
    }

    /**
     * 赛事名称 对应的大赛事, 赛事名称对应押注信息
     *
     * @param
     * @return 赛事名称 对应的大赛事, 赛事名称对应押注信息
     * @throws Exception
     */
    public static Map<String, Object> getGameInfo(MultipartFile f) throws Exception {

        String name = f.getOriginalFilename();
        String extString = name.substring(name.lastIndexOf("."));
        Map<String, Object> objects = new HashMap<String, Object>();
        String message = "";
        int result = 1;
        Map getMap = GetMap();
        Workbook wb = null;
        Sheet sheet = null;
        Sheet sheet1 = null;
        Row row = null;
        String cellData = null;
        InputStream in = f.getInputStream();


        if (".xls".equals(extString)) {
            wb = new HSSFWorkbook(in);
        } else if (".xlsx".equals(extString)) {
            wb = new XSSFWorkbook(in);
        }

        if (wb != null) {

            // 获取第一个sheet
            sheet = wb.getSheetAt(0);
            sheet1 = wb.getSheetAt(1);
            // 获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            int rownum1 = sheet1.getPhysicalNumberOfRows();

            // 获取最大列数
            Map<String, Object> bigcMap = new HashMap<>();
            List<GameInfo> gameinfos = new ArrayList<>();
            for (int i = 1; i < rownum; i++) {
                GameInfo gameInfo = new GameInfo();
                row = sheet.getRow(i);
                if (row != null) {
                    String gamename = (String) getCellFormatValue(row.getCell(0));
                    gameInfo.setGameName(gamename);
                    String country = (String) getCellFormatValue(row.getCell(1));
                    Integer countInt = (Integer) getMap.get(country);
                    gameInfo.setCountry(String.valueOf(countInt));
                    String betname = (String) getCellFormatValue(row.getCell(2));
                    gameInfo.setBetName(betname);
                    Date gameTime = row.getCell(3).getDateCellValue();
                    gameInfo.setGameTime(gameTime);


                    gameinfos.add(gameInfo);
                }
            }
            // 赛事名称 对应的
            Map<String, List<GameInfo>> gaMap = new HashMap<>();
            for (GameInfo info : gameinfos) {
                String gameName = info.getGameName();
                if (gaMap.get(gameName) != null) {
                    List<GameInfo> list = gaMap.get(gameName);
                    list.add(info);
                } else {
                    List<GameInfo> list = new ArrayList<>();
                    list.add(info);
                    gaMap.put(info.getGameName(), list);
                }
            }


            Set<String> keySet2 = gaMap.keySet();
            for (String s2 : keySet2) {
                Random random = new Random();
                int nextInt = random.nextInt(10000000);
                List<GameInfo> list = gaMap.get(s2);
                for (GameInfo gameInfo : list) {
                    gameInfo.setOutId(String.valueOf(100000000 + nextInt));
                }
            }

            // 押注项中 赛事名称对应的押注项
            Map<String, List<ExceBetInfoVo>> eMap = new HashMap<>();
            ;


            for (int i = 1; i < rownum1; i++) {
                row = sheet1.getRow(i);
                if (row != null) {
                    ExceBetInfoVo p = new ExceBetInfoVo();

                    String betname = (String) getCellFormatValue(row.getCell(0));

                    String g = (String) getCellFormatValue(row.getCell(1));

                    String t = (String) getCellFormatValue(row.getCell(2));

                    String plname = (String) getCellFormatValue(row.getCell(3));
                    String c = NumberToTextConverter.toText(row.getCell(4).getNumericCellValue());


                    Random random = new Random();
                    int nextInt = random.nextInt(10000000);
                    p.setC(Double.parseDouble(c));
                    p.setG(g);
                    p.setI(Long.valueOf(100000000 + nextInt));
                    p.setN(plname);


                    p.setT(t);

                    if (eMap.get(betname) != null) {
                        List<ExceBetInfoVo> list = eMap.get(betname);
                        list.add(p);
                    } else {
                        List<ExceBetInfoVo> list = new ArrayList<>();
                        list.add(p);
                        eMap.put(betname, list);
                    }
                }

            }


            //赛事名 对应的押注项的 集合
            Map<String, OtherBet> betMaps = null;
            if (eMap != null) {

                betMaps = new HashMap<String, OtherBet>();
                Set<String> keySet = eMap.keySet();
                for (String key : keySet) {
                    // 押注类型 G 对应的押注项
                    Map<String, List<ExceBetInfoVo>> gmap = new HashMap<>();
                    //一个赛事押注信息
                    OtherBet otherBet = new OtherBet();
                    // 赛事名 对应的押注项
                    List<ExceBetInfoVo> list = eMap.get(key);
                    //一个赛事对应的所有押注项
                    for (ExceBetInfoVo vo : list) {
                        if (gmap.get(vo.getG()) != null) {
                            List<ExceBetInfoVo> list2 = gmap.get(vo.getG());
                            list2.add(vo);
                        } else {
                            List<ExceBetInfoVo> list2 = new ArrayList<>();
                            list2.add(vo);
                            gmap.put(vo.getG(), list2);
                        }
                    }

                    Set<String> gsets = gmap.keySet();


                    GE ge1 = null;
                    GE ge2 = null;
                    GE ge3 = null;
                    GE ge4 = null;
                    GE ge5 = null;
                    GE ge6 = null;
                    GE ge7 = null;
                    GE ge8 = null;
                    for (String g : gsets) {

                        switch (g) {
                            case "主客队":
                                if (ge1 == null) {
                                    ge1 = new GE();
                                }
                                List<List<betPlayer>> es1 = new ArrayList<>();
                                List<betPlayer> e11 = null;
                                List<betPlayer> e12 = null;
                                List<betPlayer> e13 = null;
                                //G 类型的所有押注项
                                List<ExceBetInfoVo> list2 = gmap.get(g);
                                for (ExceBetInfoVo vo : list2) {
                                    betPlayer b1 = new betPlayer();
                                    b1.setC(vo.getC());
                                    player pp1 = new player();
                                    pp1.setI(vo.getI());
                                    pp1.setN(vo.getN());
                                    b1.setPL(pp1);
                                    switch (vo.getT()) {
                                        case "主队赢":
                                            if (e11 == null) {
                                                e11 = new ArrayList<>();
                                            }
                                            b1.setT(Long.valueOf(1702));
                                            player pp = new player();
                                            pp.setI(vo.getI());
                                            pp.setN(vo.getN());
                                            b1.setPL(pp);
                                            e11.add(b1);
                                            break;
                                        case "客队赢":
                                            if (e12 == null) {
                                                e12 = new ArrayList<>();
                                            }
                                            b1.setT(Long.valueOf(1703));
                                            player pp11 = new player();
                                            pp11.setI(vo.getI());
                                            pp11.setN(vo.getN());
                                            b1.setPL(pp11);
                                            e12.add(b1);
                                            break;
                                        case "平局":
                                            if (e13 == null) {
                                                e13 = new ArrayList<>();
                                            }
                                            b1.setT(Long.valueOf(1704));
                                            player pp2 = new player();
                                            pp2.setI(vo.getI());
                                            pp2.setN(vo.getN());
                                            b1.setPL(pp2);
                                            e13.add(b1);
                                            break;
                                        default:
                                            result = -1;
                                            message = message + "押注类型:  " + g + "中具体押注类型:  " + vo.getT() + "未匹配;";
                                            break;
                                    }
                                    if (e11 != null) {
                                        es1.add(e11);
                                    }
                                    if (e12 != null) {
                                        es1.add(e12);
                                    }
                                    if (e13 != null) {
                                        es1.add(e13);
                                    }
                                    ge1.setE(es1);
                                    ge1.setG(Long.valueOf(837));
                                }
                                break;
                            case "获胜者在回合":
                                if (ge2 == null) {
                                    ge2 = new GE();
                                }
                                List<List<betPlayer>> es2 = new ArrayList<>();
                                List<betPlayer> e21 = null;
                                List<ExceBetInfoVo> list3 = gmap.get(g);
                                for (ExceBetInfoVo vo : list3) {
                                    if (e21 == null) {
                                        e21 = new ArrayList<>();
                                    }
                                    betPlayer b1 = new betPlayer();
                                    b1.setC(vo.getC());
                                    b1.setT(Long.valueOf(2486));
                                    player pp = new player();
                                    pp.setI(vo.getI());
                                    pp.setN(vo.getN());
                                    b1.setPL(pp);
                                    e21.add(b1);
                                }
                                if (e21 != null) {
                                    es2.add(e21);
                                }
                                ge2.setE(es2);
                                ge2.setG(Long.valueOf(2257));
                                break;
                            case "谁会赢":
                                if (ge3 == null) {
                                    ge3 = new GE();
                                }
                                List<List<betPlayer>> es3 = new ArrayList<>();
                                List<betPlayer> e31 = null;
                                List<ExceBetInfoVo> list4 = gmap.get(g);
                                for (ExceBetInfoVo vo : list4) {
                                    if (e31 == null) {
                                        e31 = new ArrayList<>();
                                    }
                                    betPlayer b1 = new betPlayer();
                                    b1.setC(vo.getC());
                                    b1.setT(Long.valueOf(835));
                                    player pp = new player();
                                    pp.setI(vo.getI());
                                    pp.setN(vo.getN());
                                    b1.setPL(pp);
                                    e31.add(b1);
                                }
                                if (e31 != null) {
                                    es3.add(e31);
                                }
                                ge3.setE(es3);
                                ge3.setG(Long.valueOf(123));
                                break;
                            case "在top5":
                                if (ge4 == null) {
                                    ge4 = new GE();
                                }
                                List<List<betPlayer>> es4 = new ArrayList<>();
                                List<betPlayer> e41 = null;
                                List<ExceBetInfoVo> list5 = gmap.get(g);
                                for (ExceBetInfoVo vo : list5) {
                                    if (e41 == null) {
                                        e41 = new ArrayList<>();
                                    }
                                    betPlayer b1 = new betPlayer();
                                    b1.setC(vo.getC());
                                    b1.setT(Long.valueOf(3123));
                                    player pp = new player();
                                    pp.setI(vo.getI());
                                    pp.setN(vo.getN());
                                    b1.setPL(pp);
                                    e41.add(b1);
                                }
                                if (e41 != null) {
                                    es4.add(e41);
                                }
                                ge4.setE(es4);
                                ge4.setG(Long.valueOf(2590));
                                break;
                            case "在top10":
                                if (ge5 == null) {
                                    ge5 = new GE();
                                }
                                List<List<betPlayer>> es5 = new ArrayList<>();
                                List<betPlayer> e51 = null;
                                List<ExceBetInfoVo> list6 = gmap.get(g);
                                for (ExceBetInfoVo vo : list6) {
                                    if (e51 == null) {
                                        e51 = new ArrayList<>();
                                    }
                                    betPlayer b1 = new betPlayer();
                                    b1.setC(vo.getC());
                                    b1.setT(Long.valueOf(3133));
                                    player pp = new player();
                                    pp.setI(vo.getI());
                                    pp.setN(vo.getN());
                                    b1.setPL(pp);
                                    e51.add(b1);
                                }
                                if (e51 != null) {
                                    es5.add(e51);
                                }
                                ge5.setE(es5);
                                ge5.setG(Long.valueOf(2600));
                                break;
                            case "会更高":
                                if (ge6 == null) {
                                    ge6 = new GE();
                                }
                                List<List<betPlayer>> es6 = new ArrayList<>();
                                List<betPlayer> e61 = null;
                                List<betPlayer> e62 = null;
                                List<betPlayer> e63 = null;
                                List<ExceBetInfoVo> list7 = gmap.get(g);
                                for (ExceBetInfoVo vo : list7) {
                                    betPlayer b1 = new betPlayer();
                                    b1.setC(vo.getC());
                                    player pp1 = new player();
                                    pp1.setI(vo.getI());
                                    pp1.setN(vo.getN());
                                    b1.setPL(pp1);
                                    switch (vo.getT()) {
                                        case "主队赢":
                                            if (e61 == null) {
                                                e61 = new ArrayList<>();
                                            }
                                            b1.setT(Long.valueOf(3098));
                                            player pp61 = new player();
                                            pp61.setI(vo.getI());
                                            pp61.setN(vo.getN());
                                            b1.setPL(pp61);
                                            e61.add(b1);
                                            break;
                                        case "客队赢":
                                            if (e62 == null) {
                                                e62 = new ArrayList<>();
                                            }
                                            b1.setT(Long.valueOf(3099));
                                            player pp62 = new player();
                                            pp62.setI(vo.getI());
                                            pp62.setN(vo.getN());
                                            b1.setPL(pp62);
                                            e62.add(b1);
                                            break;
                                        case "W3":
                                            if (e63 == null) {
                                                e63 = new ArrayList<>();
                                            }
                                            b1.setT(Long.valueOf(3100));
                                            player pp63 = new player();
                                            pp63.setI(vo.getI());
                                            pp63.setN(vo.getN());
                                            b1.setPL(pp63);
                                            e63.add(b1);
                                            break;
                                        default:
                                            result = -1;
                                            message = message + "押注类型:  " + g + "中具体押注类型:  " + vo.getT() + "未匹配;";
                                            break;
                                    }
                                }
                                if (e61 != null) {
                                    es6.add(e61);
                                }
                                if (e62 != null) {
                                    es6.add(e62);
                                }
                                if (e63 != null) {
                                    es6.add(e63);
                                }
                                ge6.setE(es6);
                                ge6.setG(Long.valueOf(2568));
                                break;
                            case "总射门数":
                                if (ge7 == null) {
                                    ge7 = new GE();
                                }
                                List<List<betPlayer>> es7 = new ArrayList<>();
                                List<betPlayer> e71 = null;
                                List<betPlayer> e72 = null;
                                List<ExceBetInfoVo> list8 = gmap.get(g);
                                for (ExceBetInfoVo vo : list8) {
                                    betPlayer b1 = new betPlayer();
                                    b1.setC(vo.getC());
                                    player pp1 = new player();
                                    pp1.setI(vo.getI());
                                    pp1.setN(vo.getN());
                                    b1.setPL(pp1);
                                    switch (vo.getT()) {
                                        case "总击球数双数":
                                            if (e71 == null) {
                                                e71 = new ArrayList<>();
                                            }
                                            b1.setT(Long.valueOf(4934));
                                            player pp71 = new player();
                                            pp71.setI(vo.getI());
                                            pp71.setN(vo.getN());
                                            b1.setPL(pp71);
                                            e71.add(b1);
                                            break;
                                        case "总击球数单数":
                                            if (e72 == null) {
                                                e72 = new ArrayList<>();
                                            }
                                            b1.setT(Long.valueOf(4935));
                                            player pp72 = new player();
                                            pp72.setI(vo.getI());
                                            pp72.setN(vo.getN());
                                            b1.setPL(pp72);
                                            e72.add(b1);
                                            break;

                                        default:
                                            result = -1;
                                            message = message + "押注类型:" + g + "  中具体押注类型:  " + vo.getT() + "未匹配;";
                                            break;

                                    }
                                }
                                if (e71 != null) {
                                    es7.add(e71);
                                }
                                if (e72 != null) {
                                    es7.add(e72);
                                }

                                ge7.setE(es7);
                                ge7.setG(Long.valueOf(3537));
                                break;
                            case "球员总射门数":
                                if (ge8 == null) {
                                    ge8 = new GE();
                                }
                                List<List<betPlayer>> es8 = new ArrayList<>();
                                List<betPlayer> e81 = null;
                                List<betPlayer> e82 = null;
                                List<ExceBetInfoVo> list9 = gmap.get(g);
                                for (ExceBetInfoVo vo : list9) {
                                    betPlayer b1 = new betPlayer();
                                    b1.setC(vo.getC());
                                    player pp1 = new player();
                                    pp1.setI(vo.getI());
                                    pp1.setN(vo.getN());
                                    b1.setPL(pp1);
                                    switch (vo.getT()) {
                                        case "总击球数双数":
                                            if (e81 == null) {
                                                e81 = new ArrayList<>();
                                            }
                                            b1.setT(Long.valueOf(4938));
                                            player pp81 = new player();
                                            pp81.setI(vo.getI());
                                            pp81.setN(vo.getN());
                                            b1.setPL(pp1);
                                            e81.add(b1);
                                            break;
                                        case "总击球数单数":
                                            if (e82 == null) {
                                                e82 = new ArrayList<>();
                                            }
                                            b1.setT(Long.valueOf(4935));
                                            player pp82 = new player();
                                            pp82.setI(vo.getI());
                                            pp82.setN(vo.getN());
                                            b1.setPL(pp1);
                                            e82.add(b1);
                                            break;

                                        default:
                                            result = -1;
                                            message = message + "押注类型:" + g + "  中具体押注类型:  " + vo.getT() + "未匹配;";
                                            break;

                                    }
                                }
                                if (e81 != null) {
                                    es8.add(e81);
                                }
                                if (e82 != null) {
                                    es8.add(e82);
                                }
                                ge8.setE(es8);
                                ge8.setG(Long.valueOf(3541));
                                break;
                            default:
                                result = -1;
                                message = message + "押注类型:" + g + "不匹配;";
                                break;
                        }


                    }
                    List<GE> ges = new ArrayList<>();
                    if (ge1 != null) {
                        ges.add(ge1);
                    }
                    if (ge2 != null) {
                        ges.add(ge2);
                    }
                    if (ge3 != null) {
                        ges.add(ge3);
                    }
                    if (ge4 != null) {
                        ges.add(ge4);
                    }
                    if (ge5 != null) {
                        ges.add(ge5);
                    }
                    if (ge6 != null) {
                        ges.add(ge6);
                    }
                    if (ge7 != null) {
                        ges.add(ge7);
                    }
                    if (ge8 != null) {
                        ges.add(ge8);
                    }
                    if (ges != null) {
                        otherBet.setGE(ges);
                    }
                    if (otherBet != null) {
                        betMaps.put(key, otherBet);

                    }


                }

            }
            objects.put("result", result);
            objects.put("message", message);
            objects.put("gaMap", gaMap);
            objects.put("betmaps", betMaps);

            //	String jsonString = JSONObject.toJSONString(betMaps);
            //	String jsonString2 = JSONObject.toJSONString(gaMap);
            System.out.println("111");
        }

        return objects;
    }

    // 读取excel
    public static Workbook readExcel(String filePath) {
        Workbook wb = null;
        if (filePath == null) {
            return null;
        }
        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream is = null;
        try {
            InputStream in = ParseExcelUtil.class.getClassLoader().getResourceAsStream(filePath);
            if (".xls".equals(extString)) {
                return wb = new HSSFWorkbook(in);
            } else if (".xlsx".equals(extString)) {
                return wb = new XSSFWorkbook(in);
            } else {
                return wb = null;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }

    public static Object getCellFormatValue(Cell cell) {
        Object cellValue = null;
        if (cell != null) {

            cellValue = cell.toString();
		/*	if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
				 Date d = cell.getDateCellValue();
				 cellValue=d;
				 String string = d.toString();
			}*/

        } else {
            cellValue = "";
        }
        return cellValue;
    }

    public static Map GetMap() {
        String str = "{\"苏格兰\":219,\"瑞士\":206,\"日本\":218,\"比利时\":24,\"泰国\":180,\"德国\":53,\"世界\":225,\"西班牙\":78,\"捷克\":204,\"瓜德罗普\":49,\"欧洲\":223,\"意大利\":79,\"加拿大\":85,\"韩国\":215,\"南非\":214,\"波兰\":147,\"法国\":198,\"英国\":39,\"英格兰\":231,\"美国\":153}";
        String countrys = ConfigFileUtils.getPropertyString("conutrys");
        Map map = JSON.parseObject(countrys);
        return map;
    }

    public static void main(String[] args) throws Exception {


    }
}
